package com.example.assignment4.controll;


import com.example.assignment4.exception.R;
import com.example.assignment4.commodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private com.example.assignment4.service.commodityService commodityService;

    @GetMapping
    private R getAll(){
        return R.success(commodityService.getAll());
    }

    @GetMapping("/{id}")
    private R getById(@PathVariable Long id){
        return R.success(commodityService.getById(id));
    }

    @PostMapping
    private R add(@RequestBody commodity commodity){
        commodityService.add(commodity);
        return R.success("添加完成");
    }

    @PutMapping("/{id}")
    private R update(@PathVariable Long id,@RequestBody commodity commodity){
        if(id.equals(commodity.getId())){
            commodityService.updateById(commodity);
            return R.success("修改完成");
        }else {
            return R.error("ID错误");
        }
    }

    @DeleteMapping("/{id}")
    private R deleteById(@PathVariable Long id){
        commodityService.deleteById(id);
        return R.success("已删除");
    }
}
